<?
/**
 * Copyright 2007 Melange.
 *
 * This file is part of PHP-MELANGE.
 *
 * PHP-MELANGE is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * PHP-MELANGE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PHP-MELANGE; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category    Melange
 * @package     php-melange
 * @subpackage  cms
 * @copyright   Copyright (c) 2007 Jeroen Simons. All rights reserved
 * @author      Jeroen Simons <jeroen@melange.nl>
 * @link        http://www.melange.nl/
 *
 */

require_once("cms/core/helper/Abstract.php");
require_once("cms/core/helper/bean/Interface.php");

abstract class AbstractList extends PHP_CMS_View_Helper_Abstract
    implements ListInterface {

    protected $form;
    protected $query;
    protected $rows;
    protected $items;
    protected $criteria;
    protected $isInit = false;
    protected $method;

    public function isInit() {
        return isInit;
    }

    public function init($listForm) {

        $this->form = $listForm;
        $this->rows = null;
        $this->items = null;

        if($this->criteria === null)
            $this->criteria = new Criteria();

        $this->query = $this->form->getQuery();

        if(strtolower($this->query) == "yes" || strtolower($this->query) == "ja") {
            $this->query = true;
        }

        if(strtolower($this->query) == "no" || strtolower($this->query) == "nee") {
            $this->query = false;
        }

        if(strlen($this->query) > 0 || is_bool($this->query)) {
            if($this->form->getFilter() == "") {
                $this->initAllCriteria();
            } else {
                $this->initFilterCriteria();
            }
        }

        $this->isInit = true;
    }

    public function getRows() {

        if(is_null($this->rows)) {

            $c = clone $this->criteria;
            $c->clearOrderByColumns();
            $c->clearGroupByColumns();
            $this->rows = $this->getPeer()->doCount($c);
        }
        return $this->rows;

    }

    public function getItems() {

        if(strtolower($this->form->getDirection()) == "asc") {
            $this->criteria->addAscendingOrderByColumn($this->form->getOrder());
        } else {
            $this->criteria->addDescendingOrderByColumn($this->form->getOrder());
        }

        if(strlen($this->form->getResults()) > 0) {
            $this->criteria->setLimit($this->form->getResults());
            $this->criteria->setOffset($this->form->getResults() * ($this->form->getPage() - 1));
        }

        if(is_null($this->items)) {

            $peer = &$this->getPeer();
            if(method_exists($this->getPeer(), $this->getMethod())) {
                $this->items = call_user_func(
                    array($peer, $this->getMethod()), $this->criteria);
            } else {
                throw new Exception("no such method in " . get_class($this->getPeer()));
            }
        }

        return $this->items;
    }

    public function initFilterCriteria() {

        if(is_bool($this->query)) {
            $this->criteria->add($this->form->getFilter(), $this->query);
        } else if(is_numeric($this->query)) {
            $this->criteria->add($this->form->getFilter(), $this->query);
        } else {
            $this->criteria->add($this->form->getFilter(), "%".$this->query."%", Criteria::LIKE);
        }

    }

    public function initAllCriteria() {

        $fields = $this->getPeer()->getFieldNames(BasePeer::TYPE_COLNAME);
        $criterion = $this->criteria->getNewCriterion($fields[0], "", Criteria::EQUAL);
        foreach($this->getFields() as $field) {
            $criterion->addOr(
                is_bool($this->query) ? $this->criteria->getNewCriterion($field, $this->query) :
                    $this->criteria->getNewCriterion($field, "%".$this->query."%", Criteria::LIKE)
            );
        }
        $this->criteria->add($criterion);


    }

    public function getCriteria() {
        return $this->criteria;
    }

    protected function getMethod() {
        return "doSelect";
    }

}
?>
